package com.css.fxfzfxqh.modules.modelManagement.service.impl;

import cn.hutool.core.util.StrUtil;
import com.css.fxfzfxqh.modules.modelManagement.entity.EconomicLossesEntity;
import com.css.fxfzfxqh.modules.modelManagement.param.AddEconomicLossesParam;
import com.css.fxfzfxqh.modules.modelManagement.param.PagePersonnelCasualtiesParam;
import com.css.fxfzfxqh.modules.modelManagement.repository.EconomicLossesRepository;
import com.css.fxfzfxqh.modules.modelManagement.service.EconomicLossesService;
import com.css.fxfzfxqh.modules.modelManagement.vo.PagePersonnelCasualtiesVo;
import com.css.fxfzfxqh.util.CreateGson;
import com.css.fxfzfxqh.util.PlatformDateUtils;
import com.css.fxfzfxqh.util.UUIDGenerator;
import com.css.fxfzfxqh.web.PlatformSessionContext;
import com.css.fxfzfxqh.zcpt.sys.entity.SUser;
import com.css.fxfzfxqh.zcpt.sys.service.SUserService;
import com.css.fxfzfxqh.zcpt.sys.service.SysAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class EconomicLossesServiceImpl implements EconomicLossesService {

    @Autowired
    private EconomicLossesRepository economicLossesRepository;

    @Autowired
    private SUserService userService;

    @Autowired
    private SysAreaService areaService;

    @Override
    public int getSizeByName(String modelName) {
        return economicLossesRepository.getSizeByName(modelName);
    }

    @Override
    public void save(AddEconomicLossesParam param) {
        EconomicLossesEntity entity = paramToEntity(param);
        entity.setId(UUIDGenerator.getUUID());
        String userId = PlatformSessionContext.getUserID();
        entity.setCreateUserId(userId);
        String user = userService.getSUser(userId);
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        entity.setCreateUserName(sUser.getUserName());
        entity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        entity.setOrgId(sUser.getOrgId());
        entity.setOrgName(sUser.getOrgName());
        entity.setIsValid("1");
        economicLossesRepository.save(entity);
    }

    private EconomicLossesEntity paramToEntity(AddEconomicLossesParam param) {
        return EconomicLossesEntity.builder()
                .modelName(param.getModelName())
                .isDefault(param.getIsDefault())
                .province(param.getProvince())
                .city(param.getCity())
                .structureLossess(param.getStructureLossess())
                .indoorLossess(param.getIndoorLossess())
                .basic1(param.getBasic1())
                .slight1(param.getSlight1())
                .mid1(param.getMid1())
                .serious1(param.getSerious1())
                .destroy1(param.getDestroy1())
                .basic2(param.getBasic2())
                .slight2(param.getSlight2())
                .mid2(param.getMid2())
                .serious2(param.getSerious2())
                .destroy2(param.getDestroy2())
                .basic3(param.getBasic3())
                .slight3(param.getSlight3())
                .mid3(param.getMid3())
                .serious3(param.getSerious3())
                .destroy3(param.getDestroy3())
                .basic4(param.getBasic4())
                .slight4(param.getSlight4())
                .mid4(param.getMid4())
                .serious4(param.getSerious4())
                .destroy4(param.getDestroy4())
                .basic5(param.getBasic5())
                .slight5(param.getSlight5())
                .mid5(param.getMid5())
                .serious5(param.getSerious5())
                .destroy5(param.getDestroy5())
                .basic6(param.getBasic6())
                .slight6(param.getSlight6())
                .mid6(param.getMid6())
                .serious6(param.getSerious6())
                .destroy6(param.getDestroy6())
                .basic7(param.getBasic7())
                .slight7(param.getSlight7())
                .mid7(param.getMid7())
                .serious7(param.getSerious7())
                .destroy7(param.getDestroy7())
                .basic8(param.getBasic8())
                .slight8(param.getSlight8())
                .mid8(param.getMid8())
                .serious8(param.getSerious8())
                .destroy8(param.getDestroy8())
                .basic9(param.getBasic9())
                .slight9(param.getSlight9())
                .mid9(param.getMid9())
                .serious9(param.getSerious9())
                .destroy9(param.getDestroy9())
                .lifeRatio1(param.getLifeRatio1())
                .lifeRatio2(param.getLifeRatio2())
                .otherRatio1(param.getOtherRatio1())
                .otherRatio2(param.getOtherRatio2())
                .basic10(param.getBasic10())
                .slight10(param.getSlight10())
                .mid10(param.getMid10())
                .serious10(param.getSerious10())
                .destroy10(param.getDestroy10())
                .build();
    }

    @Override
    public Map<String, Object> page(PagePersonnelCasualtiesParam param, int curPage, int pageSize) {
        Map<String, Object> page = economicLossesRepository.page(param, curPage, pageSize);
        List<PagePersonnelCasualtiesVo> list = (List<PagePersonnelCasualtiesVo>) page.get("rows");
        list.forEach(it->{
            it.setProvince(areaService.getAreaNameByAreaId(it.getProvince()));
            if(StrUtil.isNotBlank(it.getCity()) && !it.getCity().equals("全部")){
                it.setCity(areaService.getAreaNameByAreaId(it.getCity()));
            }
        });
        return page;
    }


    @Override
    public void deleteById(String id) {
        economicLossesRepository.deleteById(id);
    }

    @Override
    public void updateById(AddEconomicLossesParam param) {
        EconomicLossesEntity entity = paramToEntity(param);
        entity.setId(param.getId());
        entity.setUpdateUserId(PlatformSessionContext.getUserID());
        entity.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        economicLossesRepository.updateById(entity);
    }

    @Override
    public EconomicLossesEntity getById(String id) {
        EconomicLossesEntity entity = economicLossesRepository.getById(id);
        if(entity!=null){
            entity.setProvinceName(areaService.getAreaNameByAreaId(entity.getProvince()));
            if(!entity.getCity().equals("全部")){
                entity.setCityName(areaService.getAreaNameByAreaId(entity.getCity()));
            }
            return entity;
        }else {
            return EconomicLossesEntity.builder().build();
        }
    }
}
